package com.qsl.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qsl.model.system.SysLoginLog;
import com.qsl.model.vo.SysLoginLogQueryVo;
import com.qsl.system.mapper.SysLoginlogMapper;
import com.qsl.system.service.LoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service // bean
public class LoginLogServiceImpl extends ServiceImpl<SysLoginlogMapper, SysLoginLog> implements LoginLogService {

    @Autowired
    private SysLoginlogMapper sysLoginlogMapper;

    // 条件分页查询
    @Override
    public IPage<SysLoginLog> selectPage(Integer page, Integer limit, SysLoginLogQueryVo loginLog) {

        Page<SysLoginLog> pageParam = new Page<>(page, limit);
        // 条件
        String username = loginLog.getUsername();
        String createTimeBegin = loginLog.getCreateTimeBegin();
        String createTimeEnd = loginLog.getCreateTimeEnd();
        //封装条件
        QueryWrapper<SysLoginLog> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            qw.like("username", username);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            qw.ge("create_time", createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            qw.le("create_time", createTimeEnd);
        }

        IPage<SysLoginLog> sysLoginLogPage = sysLoginlogMapper.selectPage(pageParam, qw);
        return sysLoginLogPage;
    }

    /**
     * 新增
     *
     * @param username 用户名
     * @param status   状态
     * @param Ipaddr   ip
     * @param message  消息-是否成功
     */
    @Override
    public void recordLoginLog(String username, Integer status, String Ipaddr, String message) {
        SysLoginLog loginLog = new SysLoginLog();
        loginLog.setUsername(username);
        loginLog.setStatus(status);  // 日志状态
        loginLog.setIpaddr(Ipaddr);
        loginLog.setMsg(message);

        // 新增
        sysLoginlogMapper.insert(loginLog);
    }
}
